﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using HoneyBadgersOfficePlayer.Models;

namespace HoneyBadgersOfficePlayer.Views
{
    public partial class Rooms : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<SqlParameter> parameterList = new List<SqlParameter>();
                SqlDataReader reader = SQLUtilities.RunStoredProcedure("GetRoomInfo", parameterList);

                if (reader != null)
                {
                    DataTable dt = new DataTable();
                    BoundField bf = new BoundField();
                    BoundField bf1 = new BoundField();
                    BoundField bf2 = new BoundField();
                    BoundField bf3 = new BoundField();

                    dt.Columns.Add("RoomName");
                    dt.Columns.Add("NumUsersInRoom");
                    dt.Columns.Add("MaxUsers");
                    dt.Columns.Add("Private/Public");

                    while (reader.Read())
                    {
                        string roomName = reader[0].ToString();
                        string numUsers = reader[1].ToString();
                        string maxUsers = reader[2].ToString();
                        string privPub;
                        if (reader[3].ToString() == "False")
                            privPub = "Public";
                        else
                            privPub = "Private";

                        dt.Rows.Add(
                        bf.DataField.Insert(0, roomName),
                        bf1.DataField.Insert(0, numUsers),
                        bf2.DataField.Insert(0, maxUsers),
                        bf3.DataField.Insert(0, privPub)
                        );

                    }
                    Session["CACHEDATA1"] = dt;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }

        }

        protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Join":
                    {
                        string roomName = e.CommandArgument.ToString();
                        int roomID = 0;
                        List<SqlParameter> parameterList = new List<SqlParameter>();
                        parameterList.Add(new SqlParameter("@Room_name", roomName));

                        SqlDataReader reader = SQLUtilities.RunStoredProcedure("GetRoomID", parameterList);

                        if (reader != null)
                        {
                            while (reader.Read())
                            {
                                roomID = Convert.ToInt32(reader[0]);
                                Globals.RoomID = roomID;
                            }
                        }

                        parameterList.Clear();
                        parameterList.Add(new SqlParameter("@User_id", Globals.UserID));
                        parameterList.Add(new SqlParameter("@Room_ID", Globals.RoomID));

                        SQLUtilities.RunStoredProcedure("InsertUserIntoRoom", parameterList);

                        Response.Write("<script type='text/javascript'>window.open('StreamDemo.aspx','_self');</script>");
                    }
                    break;

                default:
                    break;
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Write("<script type='text/javascript'>window.open('CreateRoom.aspx','_self');</script>");
        }

    }
}